package com.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dao.BaseDAO;
import com.entity.Stu;
import com.entity.Userinfo;
import com.entity.Yegui;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class StuAction extends ActionSupport {
	Stu data;
	BaseDAO dao = new BaseDAO();

	public String add() {
		dao.add(data);
		return show();
	}

	public String exit() {
		Userinfo users = (Userinfo) ActionContext.getContext().getSession().get("loginUser");
		String role = users.getUserType();
		if ("学生".equals(role)) {
			ActionContext.getContext().put("all",
					dao.find("from Stu where stuno=?", new Object[] { users.getLoginName() }));
			return "exit";
		} else {
			ActionContext.getContext().put("all", dao.show("from Stu"));
			return "exit";
		}
	}
	public String qc() {
		Stu stu = (Stu) dao.findByID(Stu.class, data.getId());
		stu.setStatus(2);
		dao.update(stu);
		
		Userinfo users = (Userinfo) ActionContext.getContext().getSession().get("loginUser");
		String role = users.getUserType();
		if ("学生".equals(role)) {
			ActionContext.getContext().put("all",
					dao.find("from Stu where stuno=?", new Object[] { users.getLoginName() }));
			return "qc";
		} else {
			ActionContext.getContext().put("all", dao.show("from Stu"));
			return "qc";
		}
	}

	public String out() {
		Stu stu = (Stu) dao.findByID(Stu.class, data.getId());
		stu.setStatus(2);
		dao.update(stu);
	//	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Yegui info = new Yegui();
		info.setId(stu.getId());
		info.setName(stu.getName());
		info.setDepartment(stu.getDepartment());
		info.setRoomno(stu.getRoomno());

		info.setOutTime(new Date());

		dao.add(info);
		
		Userinfo users = (Userinfo) ActionContext.getContext().getSession().get("loginUser");
		String role = users.getUserType();
		if ("学生".equals(role)) {
			ActionContext.getContext().put("all",
					dao.find("from Stu where stuno=?", new Object[] { users.getLoginName() }));
			return "exit";
		} else {
			ActionContext.getContext().put("all", dao.show("from Stu"));
			return "exit";
		}
	}

	public String del() {
		dao.del(dao.findByID(Stu.class, data.getId()));
		return show();
	}

	public String modify() {
		Stu stu = (Stu) dao.findByID(Stu.class, data.getId());
		ActionContext.getContext().put("record", stu);
		return "modify";
	}

	public String update() {
		Stu stu = (Stu) dao.findByID(Stu.class, data.getId());
		stu.setRoomno(data.getRoomno());
		stu.setArea(data.getArea());
		stu.setFlat(data.getFlat());
		dao.update(stu);
		return show();
	}
	public String update2() {
		Stu stu = (Stu) dao.findByID(Stu.class, data.getId());
		stu.setName(data.getName());
		stu.setSex(data.getSex());
		stu.setDepartment(data.getDepartment());
		stu.setTeam(data.getTeam());
		stu.setTel(data.getTel());
		stu.setRoomno(data.getRoomno());
		stu.setArea(data.getArea());
		stu.setFlat(data.getFlat());
		dao.update(stu);
		return stuShow();
	}

	public String stuShow() {
		Userinfo users = (Userinfo) ActionContext.getContext().getSession().get("loginUser");
		String role = users.getUserType();
		if ("学生".equals(role)) {
			ActionContext.getContext().put("all",
					dao.find("from Stu where stuno=?", new Object[] { users.getLoginName() }));
			return "showStu";
		} else {
			ActionContext.getContext().put("all", dao.show("from Stu"));
			return "showStu";
		}
	}
	
	
	
	public String show() {
		Userinfo users = (Userinfo) ActionContext.getContext().getSession().get("loginUser");
		String role = users.getUserType();
		if ("学生".equals(role)) {
			ActionContext.getContext().put("all",
					dao.find("from Stu where stuno=?", new Object[] { users.getLoginName() }));
			return SUCCESS;
		} else {
			ActionContext.getContext().put("all", dao.show("from Stu"));
			return SUCCESS;
		}
	}
	public String edit(){
		//Stu stu = (Stu) dao.findByID(Stu.class, data.getId());
    	Object obj = dao.findByID(Stu.class,data.getId());
    	ActionContext.getContext().put("record", obj);
    	return "edit";
    }
    
	public String find() {
		String hql = "from Stu where 1=1";
		List params = new ArrayList();
		
		if(!"".equals(data.getStuno())){
			hql+=" and stuno=?";
			params.add(data.getStuno());
		}
		if(!"".equals(data.getArea())){
			hql+=" and area=?";
			params.add(data.getArea());
		}
		if(!"".equals(data.getFlat())){
			hql+=" and flat=?";
			params.add(data.getFlat());
		}
		if(!"".equals(data.getRoomno())){
			hql+=" and roomno=?";
			params.add(data.getRoomno());
		}
		ActionContext.getContext().put("all", dao.find(hql, params.toArray()));
		return "stuno";
	}
	
	
	public Stu getData() {
		return data;
	}

	public void setData(Stu data) {
		this.data = data;
	}
}
